<?php

namespace App\Admin\Controllers;

use App\Models\Album;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class AlbumController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('旅游画册')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Album);

        $grid->id('ID');
        $grid->wxuser_id()->badge();
        $grid->columns('title','nickname','address','content','zan','star');
        $grid->avatar()->image('',36,36);
        $grid->coverimage()->image('http://media.weijingguanjia.com/',80,80);
        $grid->recommend()->using([
            '1'  => '推荐',
            '0' => '一般',
        ]);
        $grid->display()->switch([
            'on'  => ['value' => 1, 'text' => '审核通过', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '未审核', 'color' => 'default'],
        ]);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Album::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Album);
        $form->text('title','标题')->rules('required');
        $form->text('address','地址')->rules('required');
        $form->text('content','动态')->rules('required|max:50');
        $form->image('coverimage','美图')->rules('required');
        $form->hidden('recommend')->default(1);
        $form->switch('display');
        $form->saved(function (Form $form){
            $form->model()->avatar= asset('/images/avatar.png');
            $form->model()->nickname='常德旅游外事侨务局';
            $form->model()->save();
        });
        return $form;
    }
}
